package cn.springcloud.alibaba.sys.service;

import cn.springcloud.alibaba.core.base.Result;
import cn.springcloud.alibaba.core.base.ResultCode;
import cn.springcloud.alibaba.sys.dao.entity.SysUser;
import cn.springcloud.alibaba.sys.dao.mapper.SysUserMapper;
import cn.springcloud.alibaba.sys.dto.SysUserDTO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SysUserDetailsService {

	@Autowired
	private SysUserMapper sysUserMapper;

	public Result<SysUserDTO> getSysUserByUserName(String username) {
		SysUser sysUser = sysUserMapper.selectOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName, username));
		if (sysUser == null) {
			return Result.error(ResultCode.RESULT_DATA_NOTFOUND);
		}
		SysUserDTO userDTO = assembleSysUserDTO(sysUser);
		return Result.success(userDTO);
	}

	private SysUserDTO assembleSysUserDTO(SysUser sysUser) {
		SysUserDTO userDTO = new SysUserDTO();
		BeanUtils.copyProperties(sysUser, userDTO);
		return userDTO;
	}
}
